package ru.pachkun.home.pages;
import static org.openqa.selenium.support.ui.ExpectedConditions.presenceOfElementLocated;

import java.util.NoSuchElementException;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;

public class AddFilmPage extends Page {
	
	@FindBy(name="name")
	public WebElement name;
	
	@FindBy(name="year")
	public WebElement year;
	
	@FindBy(name="languages")
	public WebElement languages;
	
	@FindBy(name="plots")
	public WebElement plots;
	
	@FindBy(id="submit")
	public WebElement saveSubmitButton;
	
	@FindBy(css="input.error")
	public WebElement error; 
	
	public AddFilmPage(PageManager pages) {
		super(pages);
	}

	@Override
	public AddFilmPage ensurePageLoaded() {
		super.ensurePageLoaded();
		wait.until(presenceOfElementLocated(By.cssSelector(".addmovie")));
		return this;
	}

	public Boolean isError() {
		try {
			error.getLocation();
			return true;
		} catch (NoSuchElementException e) {
			return false;
		}
	}
	
	public AddFilmPage setName(String text) {
		name.sendKeys(text);
		return this;
	}

	public AddFilmPage setYear(String text) {
		year.sendKeys(text);
		return this;
	}

	public AddFilmPage setLanguages(String text) {
		languages.sendKeys(text);
		return this;
	}

	public AddFilmPage setPlots(String text) {
		plots.sendKeys(text);
		return this;
	}
	
	public FilmInfoPage clickSaveSubmitButton() {
		saveSubmitButton.click();
		return pages.filmInfoPage;
	}


	
}
